package com.qfedu.buy.server.core.alipay;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.*;
import com.alipay.api.response.*;

/**
 * @program: NeighborBuy
 * @description:
 * @author: Feri(邢朋辉)
 * @create: 2021-01-28 15:18
 */
public class AliPayUtil {

    private static final String URL="https://openapi.alipay.com/gateway.do";
    private static final String FORMAT="json";
    private static final String CHARSET="UTF-8";
    private static final String SIGN_TYPE="RSA2";
    private static final String APP_ID="2017091608770636";
    private static final String APP_PRIVATE_KEY="MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCh1qI8uo1qhrcePsa5JUAoYUX8HfPuBt7kc90aCP1M/v61/uzaU/lyGQeChKV3jdDTn2Lcq6kT5JBl3TLiaYHmO6cId1nQAIUxiT9zhB9crc4wAx8CVabMbcqUefs7Xsp+YhhUgU5X6GOS3emkUeL7RegBnL8vayfEBeUDgBxsk/K/VygBA8sapsEhnoOrB6bhMY4GaJrxb0kg9Ej8x4kpExLcxkT+UgcOiJvh6vpBZo5CJsiPQkFvSsNsWY2uSDudSL/KqpMxz+yPfVvZDt4fOfyi+CfYR43Jlo4tsT7joqH2JT06BH+KdJyc1D3Lqw7w/WdmZtmoLghH0kRZawrLAgMBAAECggEAYYtpm+rhQ7zQ8HTr+DogknYW5Z/0H5qai93d/Uw/yEHFqlJt1iZZKlE1upBS311l6beesdzxeuD/u7X4bokjV27K/YpaYsl9fl74FJslAApuRXgMH68aawsd2CIxsBYxPL3JZl3Np6SVJ7eDlJwakFMRRK+CeIVAoaDf6R01hKctkYnnE0wT+ffQNKWsISoEyiKVT3g5fur7iPOuDlDXsfi6Mm+e75wCXTmRRHmb8lPBAMLV+Kj5DFxg8dwNz81Fs4ZM2Aq0lBaTfy1H1zSlM1m42wcsMYDcgdEH9aq+OgqK+cny6umgs7/Alg7IgV/9b7AhKdvAqLy2ERUJtooj2QKBgQDe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aqdH0SoVMpxFdOpUftNdXHO+G0ZpS/7c1zpn8G64zN5J17IFrLcUlsEnSgOrJxsS2Q50b44er0KQlj76pehB2sTveHS2vdhqXzrv+oq99XtUKEY1a3nwDjXneI7YYKLHD9KU53pti/ibLDkOEjO4+DRowd+wfSwkmWGVL3X320mvCfrg/aMN71B/cyyhW0mQ4cxqh2UcnpxLm0v/+uC7dSCyAJwIDAQAB";

    private static AlipayClient alipayClient ;
    static {
        alipayClient=new DefaultAlipayClient(URL,APP_ID,APP_PRIVATE_KEY,FORMAT,CHARSET,ALIPAY_PUBLIC_KEY,SIGN_TYPE);
    }
    /**
     * 生成预支付链接 -二维码串码*/
    public static String createPay(AliPay pay){
        //创建请求对象
        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
        //设置请求参数 json格式
        request.setBizContent(JSON.toJSONString(pay));
        //获取请求响应
        try {
            AlipayTradePrecreateResponse response = alipayClient.execute(request);
            //验证请求是否成功
            if(response.isSuccess()){
                //返回预支付链接
                return response.getQrCode();
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 查询支付结果
     * 交易状态：
     * WAIT_BUYER_PAY（交易创建，等待买家付款）、
     * TRADE_CLOSED（未付款交易超时关闭，或支付完成后全额退款）、
     * TRADE_SUCCESS（交易支付成功）、
     * TRADE_FINISHED（交易结束，不可退款）*/
    public static String queryPay(String oid){
        //创建请求对象
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        //设置请求参数
        request.setBizContent("{\"out_trade_no\":\""+oid+"\"}");
        try {
            //发起请求获取响应对象
            AlipayTradeQueryResponse response = alipayClient.execute(request);
            if(response.isSuccess()){
                //返回交易的支付状态
                return response.getTradeStatus();
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 发起退款*/
    public static boolean refoundPay(AliPayRefound refound){
        //请求对象
        AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
        //设置请求参数
        request.setBizContent(JSON.toJSONString(refound));
        try {
            //获取响应对象
            AlipayTradeRefundResponse response = alipayClient.execute(request);
            //验证请求是否成功
            if(response.isSuccess()) {
                return true;
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return false;
    }
    /**
     * 查询退款请求*/
    public static boolean queryRefound(String oid,String roid){
        //实例化请求对象
        AlipayTradeFastpayRefundQueryRequest request = new AlipayTradeFastpayRefundQueryRequest();
        //设置请求参数
        request.setBizContent("{\"out_trade_no\":\""+oid+"\",\"out_request_no\":\""+roid+"\"}");
        //获取请求响应
        try {
            AlipayTradeFastpayRefundQueryResponse response = alipayClient.execute(request);
            if(response.isSuccess()){
                String r=response.getRefundStatus();
                if(r==null){
                    return true;
                }else if(r.equals("REFUND_SUCCESS")){
                    return true;
                }
                return false;
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return false;

    }
    /**
     * 关闭支付*/
    public static boolean closePay(String oid){
        AlipayTradeCloseRequest request = new AlipayTradeCloseRequest();
        request.setBizContent("{\"out_trade_no\":\""+oid+"\"}");
        try {
            AlipayTradeCloseResponse response = alipayClient.execute(request);
            if(response.isSuccess()){
               return response.getCode().equals("10000");
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return false;
    }
}
